/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.agent.goal;

import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;

import ca.usherbrooke.agent.WorldUpdaterAgent;
import ca.usherbrooke.behaviours.goal.GoalResponderBehaviour;
import ca.usherbrooke.behaviours.goal.SubscriptionEntityGoalBehaviour;
import ca.usherbrooke.behaviours.goal.UpdateGoalBehaviour;
import ca.usherbrooke.goal.BasicGoal;
import ca.usherbrooke.goal.EntityGoal;
import ca.usherbrooke.model.world.IWorldModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public class BasicGoalAgent extends WorldUpdaterAgent implements Observer {
	private static final long serialVersionUID = 1L;

	private HashSet<EntityGoal> allGoals;
	private SubscriptionEntityGoalBehaviour goalNotifier;

	@Override
	protected void setup() {
		super.setup();
		allGoals = new HashSet<EntityGoal>();

		goalNotifier = new SubscriptionEntityGoalBehaviour(this);

		this.addBehaviour(new GoalResponderBehaviour(this));
		this.addBehaviour(goalNotifier);
	}

	@Override
	protected void initWorld() {
		this.addBehaviour(new UpdateGoalBehaviour(this));
	}

	public IWorldModel getWorld() {
		return world;
	}

	public HashSet<EntityGoal> getAllEntityGoal() {
		return this.allGoals;
	}

	public void addGoal(BasicGoal goal) {
		Boolean isAdded = false;
		Long guid = goal.getEntity();

		for (EntityGoal entityGoal : allGoals) {
			if (entityGoal.getEntityGuid().equals(guid)) {
				entityGoal.addGoal(goal);
				isAdded = true;
			}
		}

		if (!isAdded) {
			EntityGoal entity = new EntityGoal(goal.getEntity());
			entity.addObserver(this);
			allGoals.add(entity);
			entity.addGoal(goal);
		}
	}

	public Boolean removeGoal(BasicGoal goal) {
		Boolean doesRemove = false;
		Long guid = goal.getEntity();
		for (EntityGoal entityGoal : allGoals) {
			if (entityGoal.getEntityGuid().equals(guid)) {
				doesRemove = entityGoal.removeGoal(goal);
			}
		}
		return doesRemove;
	}

	// /////////////////////// GETTERS ////////////////////
	/**
	 * 
	 * @param entityGuid
	 * @return the EntityGoal
	 */
	public EntityGoal getEntityGoal(Long entityGuid) {
		EntityGoal entityGoal = new EntityGoal(entityGuid);
		for (EntityGoal currentEntity : allGoals) {
			if (entityGoal.getEntityGuid().equals(entityGuid)) {
				entityGoal = currentEntity;
				break;
			}
		}

		return entityGoal;
	}

	/**
	 * 
	 * @param entityGuid
	 * @return the score of the entity
	 */
	public Float getScore(Long entityGuid) {
		Float score = 0f;
		for (EntityGoal entityGoal : allGoals) {
			if (entityGoal.getEntityGuid().equals(entityGuid)) {
				score = entityGoal.getScore();
			}
		}
		return score;
	}

	@Override
	public void update(Observable o, Object arg) {
		EntityGoal goal = (EntityGoal) o;
		goalNotifier.goalUpdate(goal.getEntityGuid());
	}
}
